package com.asf.sofom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.asf.sofom.domain.FormBancos;
import com.asf.sofom.domain.OBancos;
import com.asf.sofom.repository.BancosDao;

@Controller
@SessionAttributes
@RequestMapping(value = "/bancos")
public class BancosFormController {
	@Autowired
	BancosDao bancosDao;
		
	@RequestMapping(method = RequestMethod.POST, params = "Guardar")
	public String guardaBanco(@ModelAttribute("altaBanco")FormBancos altaBanco, BindingResult result){
		System.out.println("Guarda");
		OBancos banco = new OBancos();
		banco.setNombre(altaBanco.getNombre());
		banco.setRfc(altaBanco.getRfc());
		banco.setPais(altaBanco.getPais());
		bancosDao.setBancos(banco);
		return "/bancos";
	}
	
	@RequestMapping(method = RequestMethod.POST, params = "Cancelar")
	public String cancelar(){
		return "redirect:admin";
	}	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView loadFormAltaBancos(){
		FormBancos banco = new FormBancos();
		ModelAndView mv = new ModelAndView();
		mv.setViewName("bancos");
		mv.addObject("altaBanco", banco);
		return mv;
	}
		
}
